package com.way.capture.utils;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.way.capture.R;
import com.way.capture.data.DataInfo;
import com.way.capture.utils.glide.GlideHelper;

import java.io.File;


public class ItemViewUtil {

    public static ViewGroup inflatePhotoView(ViewGroup container) {
        return (ViewGroup) LayoutInflater.from(container.getContext())
                .inflate(R.layout.photo_view, container, false);
    }

    public static ViewGroup inflateVideoView(ViewGroup container) {
        return (ViewGroup) LayoutInflater.from(container.getContext())
                .inflate(R.layout.video_view, container, false);
    }

    public static void bindSubsamplingImageView(SubsamplingScaleImageView imageView, DataInfo albumItem,
                                                SubsamplingScaleImageView.DefaultOnImageEventListener onImageEventListener) {
        imageView.recycle();

//        ImageViewState imageViewState = null;
//        if (photo.getImageViewSavedState() != null) {
//            imageViewState = (ImageViewState) photo.getImageViewSavedState();
//            photo.putImageViewSavedState(null);
//        }

        imageView.setImage(ImageSource.uri(Uri.fromFile(new File(albumItem.path))));

        if (onImageEventListener != null) {
            imageView.setOnImageEventListener(onImageEventListener);
        }
    }

    public static void bindTransitionView(final ImageView imageView, final DataInfo albumItem) {
        //handle timeOut
        if (albumItem.isSharedElement) {
            new Handler().postDelayed(() -> {
                albumItem.isSharedElement = false;
                ((AppCompatActivity) imageView.getContext())
                        .startPostponedEnterTransition();
            }, 100);
        }
        ViewCompat.setTransitionName(imageView, albumItem.path);
        GlideHelper.loadResourceBitmap(albumItem.path, imageView, new RequestListener<Bitmap>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model,
                                        Target<Bitmap> target, boolean isFirstResource) {
                if (albumItem.isSharedElement) {
                    albumItem.isSharedElement = false;
                    ((AppCompatActivity) imageView.getContext())
                            .startPostponedEnterTransition();
                }
                return false;
            }

            @Override
            public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target,
                                           DataSource dataSource, boolean isFirstResource) {
                if (albumItem.isSharedElement) {
                    albumItem.isSharedElement = false;
                    ((AppCompatActivity) imageView.getContext())
                            .startPostponedEnterTransition();
                }
                return false;
            }
        });
    }

}
